package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.Order;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.OrderService;
import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.util.MsgPushUtils;
import com.runfast.waimai.util.SingleSendSms;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/driver/*")
public class DriverShopperTimeoutController extends DriverBaseController{
    @Resource
    private RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Resource
    private RunfastBusinessService runfastBusinessService;
    @Resource
    OrderService orderService;
    @PostMapping("timeout")
    public @ResponseBody ModelMap timeout(Integer id, String Time,String ordercode){
        //id 商家ID ,time商家接收预定单的时间
        ModelMap model=new ModelMap();
        RunfastGoodsSellRecord runfastGoodsSellRecord=new RunfastGoodsSellRecord();
        RunfastBusiness runfastBusiness=runfastBusinessService.findById(id);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  statu 0营业1休息//商家设置
        if(runfastBusiness.getStatu()==0) {

            try {

                Date date = format.parse(Time);
                runfastGoodsSellRecord.setBusinessid(id);
                runfastGoodsSellRecord.setDistime(date);
                runfastGoodsSellRecord.setOrdercode(ordercode);
                runfastGoodsSellRecordService.insertSelective(runfastGoodsSellRecord);//生成预定单
                RunfastGoodsSellRecord runfastGoodsSellRecord1 = runfastGoodsSellRecordService.findByOrderCode(ordercode);//预定单信息
                //time-1小时，将商家接单状态改为待配送，指定骑手
                 Calendar Cal=Calendar.getInstance();
                    Cal.setTime(date);
                    Cal.add(java.util.Calendar.HOUR_OF_DAY,-1); //预定时间 -1小时
               //     System.out.println("date:"+Cal.getTime());
                    if(new Date()==Cal.getTime()){
                        runfastGoodsSellRecord1.setStatus(2);//设为商家已接单，设置商家等信息
                        runfastGoodsSellRecord1.setCreatetime(Cal.getTime());
                        runfastGoodsSellRecord1.setUseraddresslat(runfastBusiness.getLatitude());
                        runfastGoodsSellRecord1.setUseraddresslng(runfastBusiness.getLongitude());
                        runfastGoodsSellRecord1.setUseraddress(runfastBusiness.getAddress());
                        runfastGoodsSellRecord1.setAgentname(runfastBusiness.getAgentname());
                        runfastGoodsSellRecord1.setAgentid(runfastBusiness.getAgentid());

                        runfastGoodsSellRecordService.updateByPrimaryKeySelective(runfastGoodsSellRecord1);//更新到数据库
                        model.put("success",true);
                        model.put("msg","商家已接单");
                    }else {
                        model.put("success",true);
                        model.put("msg","商家还没接单");
                    }

            } catch (ParseException e) {
                e.printStackTrace();

            }


        }


        return model;
    }
    /*预送订单查询
    *
    * */
    @PostMapping ("orderTimeout")
    public @ResponseBody ModelMap  orderTimeout(String token) throws ParseException {
        ModelMap model=new ModelMap();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.HOUR,+1);

        Date dt1=rightNow.getTime();
        System.out.println(dt1+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
   //     String reStr = sdf.format(dt1);
        RunfastShopper shopper=super.getLoginShopper(token);

        List<RunfastGoodsSellRecord> list= runfastGoodsSellRecordService.findBookTime(shopper.getId(),dt1,shopper.getAgentId());//查询当前时间的前一个小时间的定单
                if(list !=null){
                    for(RunfastGoodsSellRecord list1:list){
                        Long aa= list1.getBookTime().getTime()-new Date().getTime();
                        Long aaa=aa/60/1000;
                        list1.setDaojiTime(aaa);
                    }
                }
           if(list.size()==0){
               model.put("success",false);
               model.put("msg","没有预送单");
           }else {


               model.put("success",true);
               model.put("list",list);
           }

        return  model;
    }
  /*  跑腿预送订单查询
     *
     * */
    @PostMapping ("paotuiOrderTimeout")
    public @ResponseBody ModelMap  paotuiOrderTimeout(String token) throws ParseException {
        ModelMap model=new ModelMap();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();

        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.HOUR,-1);

        Date dt1=rightNow.getTime();

        RunfastShopper shopper=super.getLoginShopper(token);

        List<Order> list= orderService.findBookTime(shopper.getId(),dt1,shopper.getAgentId());//查询当前时间的前一个小时间的定单
        if(list.size()==0){
            model.put("success",false);
            model.put("msg","没有预送单");
        }else {


            model.put("success",true);
            model.put("msg",list);
        }

        return  model;
    }
}
